//  页面路径：store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import {
	BASE
} from '@/const/base.js'
import createPersistedstate from 'vuex-persistedstate'
import {
	localStorage
} from 'mp-storage'
import {
	getUserType
} from '@/utils/index.js'
import storage from '@/utils/storage.js'

import moduleA from '@/store/modules/moduleA'
import moduleB from '@/store/modules/moduleB'

Vue.use(Vuex)
export default new Vuex.Store({
	plugins: [
		createPersistedstate({
			storage: localStorage,
			paths: ['moduleB'], // 要把那些模块加入缓存
			// reducer: (val) => {
			// 	return {
			// 		num: val.num
			// 	}
			// }
		})
	],
	state: {
		token: storage.getItem('token'),
		appType: process.env.UNI_APP_TYPE || BASE.USER_TYPE,
		userType: storage.getItem('userType') ? storage.getItem('userType') : process.env.UNI_APP_TYPE == BASE
			.ADMIN_TYPE ? 3 : 4, // 用户类型 1.导购员 2.店长 3.经销商 4.普通用户 5.业务员 6.业务主管 7.业务总监 8.财务 9.库管
		userInfo: storage.getItem('basicUnserInfo') ? storage.getItem('basicUnserInfo') : {},
		bottomArea: 0,
		tabbarHeight: 0,
		// appId: uni.getAccountInfoSync().miniProgram.appId,
		functionRange: storage.getItem('tenantId') ? storage.getItem('tenantId') : 1, // 经销商小程序Id 服务端传1
		// appId:"wxdd90ffc3848954d8"
		subcribeImg: '',
		platformInfo: storage.getItem('platformInfo') ? storage.getItem('platformInfo') : {},
		dealInfo: {},
		showModalReady: true,
	},
	getters: {
		noLogin(state) {
			return !state.token && state.appType == BASE.ADMIN_TYPE
		},
		isVip(state) {
			return state.userInfo && state.userInfo.memberLevel && state.userInfo.memberLevel > 1
		}
	},
	mutations: {
		setToken: (state, data) => {
			console.log('setToken',data)
			state.token = data;
		},
		setUserType(state, data) {
			state.userType = data
		},
		setFunctionRange(state, data) {
			state.functionRange = data
		},
		setBottomArea(state, data) {
			state.bottomArea = data
		},
		setTabbarHeight(state, data) {
			state.tabbarHeight = data
		},
		setSubcribeImg(state, data) {
			state.subcribeImg = data
		},
		setPlatformInfo(state, data) {
			state.platformInfo = data
		},
		setDealInfo(state, data) {
			state.dealInfo = data
		},
		setUserInfo(state, data) {
			state.userInfo = data
		},
		setShowModalReady(state, data) {
			console.log('setShowModalReady', data)
			state.showModalReady = data
		},
	},
	modules: {
		moduleA,
		moduleB
	},
})